<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .false {
            color: red;
        }
        .true {
            color: green;
        }
    </style>
</head>
<body>
    <input class="reset_btn" type="button" value="重新开始一局游戏" onclick="reSet()"><br>
    请输入要猜的数字: <input class="input_num" type="text"> <input type="button" value="猜" onclick="guess()"><br>
    已经猜的次数：<span class="guess_cnt">0</span><br>
    结果: <span class="result"></span>
</body>
<script>
    let input_num = document.querySelector('.input_num');
    let guess_cnt = document.querySelector('.guess_cnt');
    let result = document.querySelector('.result');
    let ans = Math.floor(Math.random() * 1000) + 1;
    console.log(ans);
    function reSet() {
        input_num.value = '';
        guess_cnt.innerHTML = 0;
        result.innerHTML = '';
    }
    function guess() {
        guess_cnt.innerHTML = parseInt(guess_cnt.innerHTML) + 1;
        let user_num = parseInt(input_num.value);
        if(ans == user_num) {
            result.innerHTML = '你猜对了';
            result.className = 'true';
            ans = Math.floor(Math.random() * 1000) + 1;
            console.log(ans);
            return;
        }
        else if (ans > user_num) result.innerHTML = '你猜小了';
        else if (ans < user_num) result.innerHTML = '你猜大了';
        else result.innerHTML = '输入错误';
        result.className = 'false';
    }
</script>
</html>